package com.itheima.day08.myMap.test4;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class StudentDemo {
    public static void main(String[] args) {
        HashMap<Student, String> hm = new HashMap<>();
        Student s1 = new Student("张三", 23);
        Student s2 = new Student("李四", 24);
        Student s3 = new Student("王五", 25);
        hm.put(s1, "湖南");
        hm.put(s2, "四川");
        hm.put(s3, "北京");

        // 第一种方式遍历
        // 获取集合中所有的键，并存入集合
        Set<Student> keys = hm.keySet();
        // 遍历集合，用键找到对应的值
        for (Student key : keys) {
            String value = hm.get(key);
            System.out.println(key + "---" + value);
        }

        System.out.println("\n==================================\n");

        // 第二种方式遍历
        Set<Map.Entry<Student, String>> entries = hm.entrySet();

        for (Map.Entry<Student, String> entry : entries) {
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + "---" + value);
        }

        System.out.println("\n==================================\n");

        // 第三种遍历方式
        hm.forEach(new BiConsumer<Student, String>() {
            @Override
            public void accept(Student key, String value) {
                System.out.println(key + "---" + value);
            }
        });

        System.out.println("\n==================================\n");

        // 第三种遍历方式 Lambda表达式
        hm.forEach((Student key, String value) -> {
            System.out.println(key + "---" + value);
        });
    }
}
